#include "CourseCell.h"

struct _CourseCell
{
        AdwBin     parent_instance;
        GtkWidget* scroll;
        GtkWidget* vbox;
        GtkWidget* frame;

        /*小部件数组表存储*/
        GtkWidget* labels[8];
};

G_DEFINE_TYPE (CourseCell, course_cell, ADW_TYPE_BIN)

CourseCell*
course_cell_new ()
{
        return g_object_new (COURSE_TYPE_CELL, NULL);
}

static void
course_cell_class_init (CourseCellClass* klass)
{
        GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);
        // GObjectClass*   object_class = G_OBJECT_CLASS (klass);

        gtk_widget_class_set_template_from_resource (
                widget_class,
                "/org/korack/kcourse/CourseCell.ui");
        gtk_widget_class_bind_template_child (widget_class, CourseCell, scroll);
        gtk_widget_class_bind_template_child (widget_class, CourseCell, vbox);
        gtk_widget_class_bind_template_child (widget_class, CourseCell, frame);
}

static void
course_cell_set_info (CourseCell* cell, const char* course_info)
{
        ObjectStringApplication *strapp, *tmp;
        GtkWidget*               label;

        if (!course_info) {
                OBJECT_WARNING_TOOL (CourseCell,
                                     set_info,
                                     "课程信息为 NULL！\n");
                return;
        }
        for (int i = 0; cell->labels[i] != NULL && i < 8; ++i) {
                gtk_box_remove (GTK_BOX (cell->vbox), cell->labels[i]);
                cell->labels[i] = NULL;
        }

        strapp = object_string_application_new_with (course_info);
        object_string_application_set_segment_charset (strapp, '\n');

        for (int i = 0; i < 8; ++i) {
                tmp = object_string_application_get_string_before_segment (
                        strapp);
                if (tmp) {
                        label = gtk_label_new (
                                object_string_get_string (OBJECT_STRING (tmp)));
                        gtk_box_append (GTK_BOX (cell->vbox), label);
                        cell->labels[i] = label;
                        object_unref (tmp);
                } else {
                        break;
                }
        }
        object_unref (strapp);
}

void
course_cell_set_course (CourseCell*   cell,
                        ObjectString* course_info,
                        int           section,
                        bool          is_today)
{
        const char* colors[][2] = {
                {  "frame-color-green", NULL},
                {   "frame-color-qing", NULL},
                {  "frame-color-blue1", NULL},
                {"frame-color-purple1", NULL},
                {   "frame-color-orig", NULL},
                {    "frame-color-red", NULL},
                {  "frame-color-white", NULL},
                { "frame-color-yellow", NULL},
        };
        g_return_if_fail ((course_info != NULL));

        if (course_info->length == 0)
                section = 0;
        switch (section) {
        case 1:
        case 2:
                gtk_widget_set_css_classes (cell->frame, colors[0]);
                break;
        case 3:
        case 4:
                gtk_widget_set_css_classes (cell->frame, colors[1]);
                break;
        case 7:
        case 8:
                gtk_widget_set_css_classes (cell->frame, colors[2]);
                break;
        case 9:
        case 10:
                gtk_widget_set_css_classes (cell->frame, colors[3]);
                break;
        case 11:
        case 12:
                gtk_widget_set_css_classes (cell->frame, colors[4]);
                break;
        case 13:
        case 14:
                gtk_widget_set_css_classes (cell->frame, colors[5]);
                break;
        default: {
                if (is_today) {
                        gtk_widget_set_css_classes (cell->frame, colors[7]);
                        break;
                }
                gtk_widget_set_css_classes (cell->frame, colors[6]);
                break;
        }
        }
        course_cell_set_info (cell, course_info->charset);
}

void
course_cell_set_size (CourseCell* cell, int width, int height)
{
        gtk_widget_set_size_request (GTK_WIDGET (cell), width, height);
}

static void
course_cell_init (CourseCell* self)
{
        gtk_widget_init_template (GTK_WIDGET (self));

        for (int i = 0; i < 8; ++i)
                self->labels[i] = NULL;
}